package com.abel.bigwater.kmgw

import com.abel.bigwater.data.BcdString
import com.abel.bigwater.dataserver.model.PojoHelper
import com.abel.bigwater.dataserver.model.hgd.HgdUdpData
import com.abel.bigwater.dataserver.model.hgd.HgdUdpDataTests
import com.abel.bigwater.dataserver.model.hgd.HgdaoBase
import com.abel.bigwater.kmgw.dao.FlowDao
import io.netty.buffer.Unpooled
import junit.framework.Assert.assertEquals
import junit.framework.Assert.assertTrue
import org.junit.FixMethodOrder
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.MethodSorters
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.data.mongodb.core.query.Criteria
import org.springframework.data.mongodb.core.query.Query
import org.springframework.data.mongodb.core.query.isEqualTo
import org.springframework.test.context.junit4.SpringRunner

@RunWith(SpringRunner::class)
@SpringBootTest
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
class BwKmingGwApplicationTests {

    @Autowired
    var hgdDao: FlowDao? = null

    @Test
    fun contextLoads() {
    }

    @Test
    fun test11HgdFlowData() {
        var dlist = hgdDao!!.listFlowData(Query())
        lgr.info("count of hgd flow data: ${dlist?.size}")

        val cnt = hgdDao!!.removeExpired(60)
        lgr.info("count of old are removed: $cnt")

        val data = HgdUdpData(HgdaoBase.TYPE_TCP)
        val buf = Unpooled.buffer().writeBytes(BcdString.fromBcdString(HgdUdpDataTests.DEMO_HEX_STRING).hexArray)
        assertTrue(data.parseBuffer(buf, true))

        val p1 = PojoHelper.toPojo(data, null)
        hgdDao!!.removeFlowData(p1)
        val cnt1 = hgdDao?.listFlowData(Query())?.size

        hgdDao!!.upsert(p1)
        dlist = hgdDao!!.listFlowData(Query())
        val cnt2 = dlist?.size
        lgr.info("count of hgd flow data: $cnt2")
        assertEquals("should one data is added", (cnt1 ?: 0) + 1, cnt2)
    }

    @Test
    fun test12UpdateFlow() {
        val data = HgdUdpData(HgdaoBase.TYPE_TCP)
        val buf = Unpooled.buffer().writeBytes(BcdString.fromBcdString(HgdUdpDataTests.DEMO_HEX_STRING).hexArray)
        assertTrue(data.parseBuffer(buf, true))
        val p1 = PojoHelper.toPojo(data, null)

        assertTrue("should be upsert", hgdDao!!.upsert(p1) ?: 0 > 0)

        p1.sendCount += 1
        assertTrue("should one send-count be incremented", hgdDao!!.updateSendCount(p1) > 0)
    }

    @Test
    fun test13DeleteFlow() {
        val data = HgdUdpData(HgdaoBase.TYPE_TCP)
        val buf = Unpooled.buffer().writeBytes(BcdString.fromBcdString(HgdUdpDataTests.DEMO_HEX_STRING).hexArray)
        assertTrue(data.parseBuffer(buf, true))
        val p1 = PojoHelper.toPojo(data, null)

        assertTrue("should be upsert", hgdDao!!.upsert(p1) ?: 0 > 0)

        assertTrue("should one be removed", hgdDao!!.removeFlowData(p1) > 0)

        assertEquals("should be empty", 0, hgdDao!!.listFlowData(Query().addCriteria(
                Criteria.where("station").isEqualTo(p1.station)
                        .and("startTime").isEqualTo(p1.startTime)
        ))?.size)
    }

    companion object {
        private val lgr = LoggerFactory.getLogger(BwKmingGwApplicationTests::class.java)

        const val DEMO_HEX_STRING = """AB CD 00 7D 00 66 11 05 24 16 15 03 0F 02 CE 00 05
        1E 44 31 31 35 3B 31 2E 34 3B 31 2E 30 2E 31 31 20 20 20 20 20 00 00 00 00 00 00 00 00
        01 01 0B 01 00 00 37 07 00 00 38 07 00 00 48 07 00 00
        01 02 0B 00 00 00 01 02 00 00 03 02 00 00 05 02 00 00
        02 03 01 01 00
        03 04 01 01 00
        04 05 3E 4C CC CD 3E 80 00 00 3E 8F 5C 29
        05 0D 00 12 00 13 40 11
        06 0E 00 12 67 89 00 12 64 89 00 32 67 80
        6A 0D 0A AB CD"""
    }

}
